<template>
  <view>
    <up-input type="digit" :placeholder="placeholder" :disabled="!disabled" border="surround" v-model="_value"
      :customStyle="{ width: '83vw' }" @change="valueChange"></up-input>
  </view>
</template>

<script>
import {
  numToCny
} from '@/utils/tools.js';
import componentMinxins from '../ComponentMinxins';

export default {
  mixins: [componentMinxins],
  name: "TextInput",
  props: {
    value: {
      type: String,
      default: null
    },
    placeholder: {
      type: String,
      default: '请输入内容'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    render: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: null
    },
    ApplyDetailList: {
      type: Array,
      default: () => {
        return []
      }
    },
    itemId: {
      type: String,
      required: true
    },
  },
  watch: {
    _value: {
      handler (newValue) {
        let value = uni.getStorageSync('formData');
        var formItems = JSON.parse(value.formItems);
        formItems.forEach(item => {
          if (item.title == this.title) {
            item.value = newValue;
            uni.setStorageSync(item.id, newValue);
          }
        })
      }
    }
  },
  data () {
    return {
      _value: '',
      bigString: '',
    }
  },
  mounted () {
    let value = uni.getStorageSync('formData');
    let formItems = JSON.parse(value.formItems);
    formItems.forEach(item => {
      if (item.title == this.title) {
        uni.removeStorageSync(`${item.id}`);
      }
    })
    this.setechodata()
  },
  methods: {
    valueChange (e) {
      let num = numToCny(e);
      this.bigString = num
    },
    setechodata () {
      // 添加数据回显逻辑
      if (this.ApplyDetailList && this.ApplyDetailList.formData && this.itemId) {
        // 检查 formData 中是否存在当前 itemId 对应的字段 
        if (this.ApplyDetailList.formData.hasOwnProperty(this.itemId)) {
          // 获取对应的值
          const fieldValue = this.ApplyDetailList.formData[this.itemId];

          // 如果值是数组，取第一个元素（根据你的数据结构）
          if (Array.isArray(fieldValue) && fieldValue.length > 0) {
            this._value = fieldValue
          } else {
            // 直接赋值 
            this._value = fieldValue || '';
          }
        }
      }
    }
  }
}
</script>

<style></style>
